package com.centaline.androidsalesblog.fragments.list;

import android.content.Intent;
import android.view.View;

import com.baidu.mapapi.model.LatLng;
import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.sale.EsfEstateDetailActivity;
import com.centaline.androidsalesblog.adapter.EsfEstateAdapter;
import com.centaline.androidsalesblog.base.BaseFragment;
import com.centaline.androidsalesblog.constants.ExtraConstant;
import com.centaline.androidsalesblog.iml.RefreshListener;
import com.centaline.androidsalesblog.model.databaseobject.EsfEstateDo;
import com.centaline.androidsalesblog.widgets.MRecyclerView;
import com.centanet.framework.glide.GlideLoad;
import com.centanet.framework.interfaces.IRecycleViewListener;

import java.util.ArrayList;

/**
 * 小区列表
 * Created by guilin on 16/2/15.
 */
public class EsfEstateListFragment extends BaseFragment {

    private MRecyclerView mRecyclerView;

    private EsfEstateAdapter esfEstateAdapter;
    private ArrayList<EsfEstateDo> esfEstateDos = new ArrayList<>();
    private RefreshListener refreshListener;
    private int iRefreshType;

    public void setRefreshListener(RefreshListener refreshListener) {
        this.refreshListener = refreshListener;
    }

    @Override
    protected int setLayoutId() {
        return R.layout.layout_m_recyclerview;
    }

    @Override
    protected void findViews(View view) {
        mRecyclerView = (MRecyclerView) view.findViewById(R.id.mRecyclerView);
    }

    @Override
    protected void initView() {
        esfEstateAdapter = new EsfEstateAdapter(getActivity(), GlideLoad.init(this), esfEstateDos);
        mRecyclerView.setAdapter(esfEstateAdapter);

        mRecyclerView.setIRecycleViewListener(new IRecycleViewListener() {
            @Override
            public void downRefresh() {
                iRefreshType = 1;
                if (refreshListener != null)
                    refreshListener.down();
            }

            @Override
            public void upRefresh() {
                iRefreshType = 2;
                if (refreshListener != null)
                    refreshListener.up(esfEstateDos.size());
            }

            @Override
            public void onItemClick(int position) {
                startActivity(new Intent(getActivity(), EsfEstateDetailActivity.class)
                        .putExtra(ExtraConstant.CestCode, esfEstateDos.get(position).getCestCode()));
            }
        });
        startRefresh();
    }

    /**
     * 开启下拉刷新
     */
    public void startRefresh() {
        mRecyclerView.startRefresh();
    }

    /**
     * 停止刷新
     */
    public void stopRefresh(ArrayList<EsfEstateDo> temp) {
        if (iRefreshType == 1)
            esfEstateDos.clear();
        if (temp == null) {
            mRecyclerView.stopRefresh(false);
        } else {
            esfEstateDos.addAll(temp);
            mRecyclerView.stopRefresh(temp.size() == 10);
        }
    }

    /**
     * 设置定位信息
     */
    public void setLocation(LatLng latLng) {
        esfEstateAdapter.setLocation(latLng);
    }

    /**
     * 刷新失败
     */
    public void refreshError() {
        mRecyclerView.refreshError();
    }
}
